package flc.ast.fragment;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.AppActivity;
import flc.ast.activity.IconActivity;
import flc.ast.adapter.AppTabAdapter;
import flc.ast.adapter.IconTypeAdapter;
import flc.ast.bean.IconTypeBean;
import flc.ast.bean.MyAppBean;
import flc.ast.databinding.FragmentIconBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link IconFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class IconFragment extends BaseNoModelFragment<FragmentIconBinding> {

    private AppTabAdapter appTabAdapter;
    private List<MyAppBean> listApp = new ArrayList<>();
    private IconTypeAdapter iconTypeAdapter;
    private List<IconTypeBean> listType = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_icon;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivApp.setOnClickListener(this);
        mDataBinding.llApp.setOnClickListener(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDataBinding.rvAppList.setLayoutManager(gridLayoutManager);
        appTabAdapter = new AppTabAdapter();
        mDataBinding.rvAppList.setAdapter(appTabAdapter);
        appTabAdapter.setOnItemClickListener(this);
        mDataBinding.rvIconTypeList.setLayoutManager(new GridLayoutManager(mContext, 2));
        iconTypeAdapter = new IconTypeAdapter();
        mDataBinding.rvIconTypeList.setAdapter(iconTypeAdapter);
        iconTypeAdapter.addChildClickViewIds(R.id.llIconTypeItem);
        iconTypeAdapter.setOnItemChildClickListener(this);
        iconTypeAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivApp:
            case R.id.llApp:
                startActivity(AppActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == appTabAdapter) {
            startActivity(AppActivity.class);
        } else if (adapter == iconTypeAdapter) {
            startActivity(IconActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.llIconTypeItem:
                startActivity(IconActivity.class);
                break;

        }
    }

    @Override
    protected void initData() {
        getDownloadAppData();
        listType.add(new IconTypeBean(getString(R.string.icon_type1), "#000000", AppConfig.ICON_DATA1));
        listType.add(new IconTypeBean(getString(R.string.icon_type2), "#FFFFFF", AppConfig.ICON_DATA2));
        listType.add(new IconTypeBean(getString(R.string.icon_type3), "#FFCACC", AppConfig.ICON_DATA3));
        listType.add(new IconTypeBean(getString(R.string.icon_type4), "#FFD4A8", AppConfig.ICON_DATA4));
        listType.add(new IconTypeBean(getString(R.string.icon_type5), "#FAE370", AppConfig.ICON_DATA5));
        listType.add(new IconTypeBean(getString(R.string.icon_type6), "#C6DAFE", AppConfig.ICON_DATA6));
        iconTypeAdapter.setList(listType);
    }

    private void getDownloadAppData() {
        RxUtil.create(new RxUtil.Callback<List<PackageInfo>>() {
            @Override
            public void doBackground(ObservableEmitter<List<PackageInfo>> emitter) {
                List<PackageInfo> installedPackageInfoList = mContext.getPackageManager()
                        .getInstalledPackages(PackageManager.MATCH_UNINSTALLED_PACKAGES);
                emitter.onNext(installedPackageInfoList);
            }

            @Override
            public void accept(List<PackageInfo> infoList) {
                List<PackageInfo> packageInfos = new ArrayList<>();
                if (infoList != null && infoList.size() != 0) {
                    for (PackageInfo info : infoList) {
                        if (!AppUtils.isAppSystem(info.packageName)) {
                            packageInfos.add(info);
                        }
                    }
                    if (packageInfos != null && packageInfos.size() != 0) {
                        for (int i = 0; i < packageInfos.size(); i++) {
                            listApp.add(new MyAppBean(packageInfos.get(i).packageName, packageInfos.get(i).applicationInfo.sourceDir, false));
                        }
                    }
                    appTabAdapter.setList(listApp);
                }
            }
        });
    }

}